#!/usr/bin/env node

/**
 * 检查远程数据库中宝石相关表的结构
 */

const mysql = require('mysql2/promise');

const remoteDbConfig = {
    host: '43.143.253.188',
    port: 3306,
    user: 'nextjs',
    password: 'yEAiRE2jcxTyRBy5',
    database: 'nextjs',
    charset: 'utf8mb4'
};

async function checkGemTablesStructure() {
    let connection;

    try {
        console.log('🔌 连接远程数据库...');
        connection = await mysql.createConnection(remoteDbConfig);
        console.log('✅ 远程数据库连接成功');

        // 检查宝石相关表
        const gemTables = ['gem_base', 'gem_attributes', 'character_gem_inventory', 'equipment_gem_socket', 'gem_mining'];

        for (const tableName of gemTables) {
            console.log(`\n🔍 检查${tableName}表结构...`);

            try {
                const [columns] = await connection.execute(`
                    SELECT COLUMN_NAME, DATA_TYPE, IS_NULLABLE, COLUMN_DEFAULT, COLUMN_COMMENT
                    FROM information_schema.COLUMNS 
                    WHERE TABLE_SCHEMA = ? AND TABLE_NAME = ?
                    ORDER BY ORDINAL_POSITION
                `, [remoteDbConfig.database, tableName]);

                if (columns.length > 0) {
                    console.log(`📋 ${tableName}表结构:`);
                    columns.forEach(col => {
                        console.log(`   ${col.COLUMN_NAME}: ${col.DATA_TYPE} ${col.IS_NULLABLE === 'NO' ? 'NOT NULL' : 'NULL'} ${col.COLUMN_DEFAULT ? `DEFAULT ${col.COLUMN_DEFAULT}` : ''} ${col.COLUMN_COMMENT ? `COMMENT '${col.COLUMN_COMMENT}'` : ''}`);
                    });

                    // 检查现有数据
                    const [rows] = await connection.execute(`SELECT COUNT(*) as count FROM ${tableName}`);
                    console.log(`📊 总记录数: ${rows[0].count}`);

                    if (rows[0].count > 0) {
                        const [sampleRows] = await connection.execute(`SELECT * FROM ${tableName} LIMIT 2`);
                        console.log(`📋 前2条数据示例:`);
                        sampleRows.forEach((row, index) => {
                            console.log(`   记录${index + 1}:`, row);
                        });
                    }
                } else {
                    console.log(`❌ ${tableName}表不存在`);
                }
            } catch (error) {
                console.log(`❌ 检查${tableName}表失败: ${error.message}`);
            }
        }

    } catch (error) {
        console.error('❌ 检查失败:', error.message);
    } finally {
        if (connection) {
            await connection.end();
            console.log('\n🔌 数据库连接已关闭');
        }
    }
}

checkGemTablesStructure().catch(console.error);
